<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The MongoDB\Driver\ReadPreference class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb-driver-writeconcern.unserialize.html">« MongoDB\Driver\WriteConcern::unserialize</a></li>
      <li style="float: right;"><a href="mongodb-driver-readpreference.bsonserialize.html">MongoDB\Driver\ReadPreference::bsonSerialize »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.mongodb.html">MongoDB\Driver</a></li>
    <li>The MongoDB\Driver\ReadPreference class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.mongodb-driver-readpreference" class="reference">

 <h1 class="title">The MongoDB\Driver\ReadPreference class</h1>
 

 <div class="partintro"><p class="verinfo">(mongodb &gt;=1.0.0)</p>


  <div class="section" id="mongodb-driver-readpreference.intro">
   <h2 class="title">简介</h2>
   <p class="para">

   </p>
  </div>


  <div class="section" id="mongodb-driver-readpreference.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="modifier">final</span>
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">MongoDB\Driver\ReadPreference</strong>
     </span>

     <span class="oointerface"><span class="modifier">implements</span> 
       <a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span><span class="oointerface">,  <a href="class.serializable.html" class="interfacename">Serializable</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Constants */</div>
    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.rp-primary"><var class="varname">RP_PRIMARY</var></a></var><span class="initializer"> = 1</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.rp-primary-preferred"><var class="varname">RP_PRIMARY_PREFERRED</var></a></var><span class="initializer"> = 5</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.rp-secondary"><var class="varname">RP_SECONDARY</var></a></var><span class="initializer"> = 2</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.rp-secondary-preferred"><var class="varname">RP_SECONDARY_PREFERRED</var></a></var><span class="initializer"> = 6</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.rp-nearest"><var class="varname">RP_NEAREST</var></a></var><span class="initializer"> = 10</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.primary"><var class="varname">PRIMARY</var></a></var><span class="initializer"> = primary</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.primary-preferred"><var class="varname">PRIMARY_PREFERRED</var></a></var><span class="initializer"> = primaryPreferred</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.secondary"><var class="varname">SECONDARY</var></a></var><span class="initializer"> = secondary</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.secondary-preferred"><var class="varname">SECONDARY_PREFERRED</var></a></var><span class="initializer"> = secondaryPreferred</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.nearest"><var class="varname">NEAREST</var></a></var><span class="initializer"> = nearest</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.no-max-staleness"><var class="varname">NO_MAX_STALENESS</var></a></var><span class="initializer"> = -1</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.mongodb-driver-readpreference.html#mongodb-driver-readpreference.constants.smallest-max-staleness-seconds"><var class="varname">SMALLEST_MAX_STALENESS_SECONDS</var></a></var><span class="initializer"> = 90</span>;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.bsonserialize.html" class="methodname">bsonSerialize</a></span>(): <span class="type">object</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.construct.html" class="methodname">__construct</a></span>(<span class="methodparam"><span class="type"><span class="type">string</span>|<span class="type">int</span></span> <code class="parameter">$mode</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$tagSets</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>)</div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.gethedge.html" class="methodname">getHedge</a></span>(): <span class="type"><span class="type">?</span><span class="type">object</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.getmaxstalenessseconds.html" class="methodname">getMaxStalenessSeconds</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.getmode.html" class="methodname">getMode</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.getmodestring.html" class="methodname">getModeString</a></span>(): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.gettagsets.html" class="methodname">getTagSets</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.serialize.html" class="methodname">serialize</a></span>(): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-readpreference.unserialize.html" class="methodname">unserialize</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$serialized</code></span>): <span class="type"><span class="type void">void</span></span></div>

   }</div>


  </div>
  

  <div class="section" id="mongodb-driver-readpreference.constants">
   <h2 class="title">预定义常量</h2>
   <dl>

    
     <dt id="mongodb-driver-readpreference.constants.rp-primary"><strong><code>MongoDB\Driver\ReadPreference::RP_PRIMARY</code></strong></dt>

     <dd>

      <p class="para">
       All operations read from the current replica set primary. This is the
       default read preference for MongoDB.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.rp-primary-preferred"><strong><code>MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED</code></strong></dt>

     <dd>

      <p class="para">
       In most situations, operations read from the primary but if it is
       unavailable, operations read from secondary members.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.rp-secondary"><strong><code>MongoDB\Driver\ReadPreference::RP_SECONDARY</code></strong></dt>

     <dd>

      <p class="para">
       All operations read from the secondary members of the replica set.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.rp-secondary-preferred"><strong><code>MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED</code></strong></dt>

     <dd>

      <p class="para">
       In most situations, operations read from secondary members but if no
       secondary members are available, operations read from the primary.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.rp-nearest"><strong><code>MongoDB\Driver\ReadPreference::RP_NEAREST</code></strong></dt>

     <dd>

      <p class="para">
       Operations read from member of the replica set with the least network
       latency, irrespective of the member&#039;s type.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.primary"><strong><code>MongoDB\Driver\ReadPreference::PRIMARY</code></strong></dt>

     <dd>

      <p class="para">
       All operations read from the current replica set primary. This is the
       default read preference for MongoDB.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.primary-preferred"><strong><code>MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED</code></strong></dt>

     <dd>

      <p class="para">
       In most situations, operations read from the primary but if it is
       unavailable, operations read from secondary members.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.secondary"><strong><code>MongoDB\Driver\ReadPreference::SECONDARY</code></strong></dt>

     <dd>

      <p class="para">
       All operations read from the secondary members of the replica set.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.secondary-preferred"><strong><code>MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED</code></strong></dt>

     <dd>

      <p class="para">
       In most situations, operations read from secondary members but if no
       secondary members are available, operations read from the primary.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.nearest"><strong><code>MongoDB\Driver\ReadPreference::NEAREST</code></strong></dt>

     <dd>

      <p class="para">
       Operations read from member of the replica set with the least network
       latency, irrespective of the member&#039;s type.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.no-max-staleness"><strong><code>MongoDB\Driver\ReadPreference::NO_MAX_STALENESS</code></strong></dt>

     <dd>

      <p class="para">
       The default value for the <code class="literal">&quot;maxStalenessSeconds&quot;</code>
       option is to specify no limit on maximum staleness, which means that the
       driver will not consider a secondary&#039;s lag when choosing where to
       direct a read operation.
      </p>
     </dd>

    

    
     <dt id="mongodb-driver-readpreference.constants.smallest-max-staleness-seconds"><strong><code>MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS</code></strong></dt>

     <dd>

      <p class="para">
       The minimum value for the <code class="literal">&quot;maxStalenessSeconds&quot;</code> option
       is 90 seconds. The driver estimates secondaries&#039; staleness by
       periodically checking the latest write date of each replica set member.
       Since these checks are infrequent, the staleness estimate is coarse.
       Thus, the driver cannot enforce a max staleness value of less than 90
       seconds.
      </p>
     </dd>

    

   </dl>

  </div>


  <div class="section">
   <h2 class="title">更新日志</h2>
   <p class="para">
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>版本</th>
        <th>说明</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>PECL mongodb 1.7.0</td>
        <td>
         <p class="para">
          Added the
          <strong><code>MongoDB\Driver\ReadPreference::PRIMARY</code></strong>,
          <strong><code>MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED</code></strong>,
          <strong><code>MongoDB\Driver\ReadPreference::SECONDARY</code></strong>,
          <strong><code>MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED</code></strong>,
          <strong><code>MongoDB\Driver\ReadPreference::NEAREST</code></strong> constants.
         </p>
         <p class="para">
          Implements <span class="interfacename"><a href="class.serializable.html" class="interfacename">Serializable</a></span>.
         </p>
        </td>
       </tr>

       <tr>
        <td>PECL mongodb 1.2.0</td>
        <td>
         <p class="para">
          Added the
          <strong><code>MongoDB\Driver\ReadPreference::NO_MAX_STALENESS</code></strong>
          and
          <strong><code>MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS</code></strong>
          constants.
         </p>
         <p class="para">
          Implements <span class="interfacename"><a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span>.
         </p>
        </td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </div>

 </div>

 

















































 






 







<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="mongodb-driver-readpreference.bsonserialize.html">MongoDB\Driver\ReadPreference::bsonSerialize</a> — Returns an object for BSON serialization</li><li><a href="mongodb-driver-readpreference.construct.html">MongoDB\Driver\ReadPreference::__construct</a> — Create a new ReadPreference</li><li><a href="mongodb-driver-readpreference.gethedge.html">MongoDB\Driver\ReadPreference::getHedge</a> — Returns the ReadPreference's &quot;hedge&quot; option</li><li><a href="mongodb-driver-readpreference.getmaxstalenessseconds.html">MongoDB\Driver\ReadPreference::getMaxStalenessSeconds</a> — Returns the ReadPreference's &quot;maxStalenessSeconds&quot; option</li><li><a href="mongodb-driver-readpreference.getmode.html">MongoDB\Driver\ReadPreference::getMode</a> — Returns the ReadPreference's &quot;mode&quot; option</li><li><a href="mongodb-driver-readpreference.getmodestring.html">MongoDB\Driver\ReadPreference::getModeString</a> — Returns the ReadPreference's &quot;mode&quot; option as a string</li><li><a href="mongodb-driver-readpreference.gettagsets.html">MongoDB\Driver\ReadPreference::getTagSets</a> — Returns the ReadPreference's &quot;tagSets&quot; option</li><li><a href="mongodb-driver-readpreference.serialize.html">MongoDB\Driver\ReadPreference::serialize</a> — Serialize a ReadPreference</li><li><a href="mongodb-driver-readpreference.unserialize.html">MongoDB\Driver\ReadPreference::unserialize</a> — Unserialize a ReadPreference</li></ul>
</div>
</div></div></body></html>